<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class StorageConfig extends Model
{
    use HasFactory;

    protected $table = 'storage_configs';

    protected $fillable = [
        'type',
        'name',
        'is_default',
        'config',
        'status',
    ];

    protected $casts = [
        'config' => 'array',
        'is_default' => 'boolean',
        'status' => 'integer',
    ];

    // 存储类型常量
    const TYPE_LOCAL = 'local';
    const TYPE_ALIYUN_OSS = 'aliyun_oss';
    const TYPE_TENCENT_COS = 'tencent_cos';
    const TYPE_QINIU = 'qiniu';

    // 状态常量
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;

    /**
     * 获取默认存储配置
     *
     * @return StorageConfig|null
     */
    public static function getDefault()
    {
        return static::where('is_default', true)
            ->where('status', self::STATUS_ENABLED)
            ->first();
    }

    /**
     * 根据类型获取存储配置
     *
     * @param string $type
     * @return StorageConfig|null
     */
    public static function getByType(string $type)
    {
        return static::where('type', $type)
            ->where('status', self::STATUS_ENABLED)
            ->first();
    }
}

